//
//  MyNavigationBar.m
//  BaseLib
//
//  Created by CPX on 2018/5/31.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MyNavigationBar.h"
#import "Global.h"
#import "YYCategories.h"

/**
 自定义导航View
 @author yzl
 */
@interface BackButton :UIButton
@end

@implementation BackButton

- (void)layoutSubviews{
    [super layoutSubviews];
    
    
    
    if (@available(iOS 9.0, *)) {
        if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
            self.imageView.right = self.width;
            self.imageView.centerY = self.height/2;
            if (self.imageView.width == 0) {
                self.titleLabel.right = self.imageView.left;
            }
            else{
                self.titleLabel.right = self.imageView.left - 15;
            }
        }
        else{
            self.imageView.left = 0;
            self.imageView.centerY = self.height/2;

            if (self.imageView.width == 0) {
                self.titleLabel.left = self.imageView.right;
            }
            else{
                self.titleLabel.left = self.imageView.right + 15;
            }
        }
    } else {
        // Fallback on earlier versions
    }
    
    self.titleLabel.centerY = self.imageView.centerY;
}

@end

@implementation MyNavigationBar

- (instancetype)initWithFrame:(CGRect)frame{
    
    CGRect rect = CGRectMake(0, 0, kScreenWidth, NavHeight);
    self = [super initWithFrame:rect];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        return self;
    }
    return nil;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.backButton sizeToFit];
    self.backButton.height = 24;
    self.backButton.left = 16;
    self.backButton.bottom = self.height - 8;
    if (@available(iOS 9.0, *)) {
        if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
            self.backButton.right = self.width - 16;
            
            
            
            if (!_enableCustomFrameRightButtons) {
                if (_rightButton2) {
                    //            _rightButton2.size = CGSizeMake(24, 24);
                    [_rightButton2 sizeToFit];
                    _rightButton2.left = 16;
                    _rightButton2.centerY = self.backButton.centerY;
                }
                if (_rightButton1) {
                    //            _rightButton1.size = CGSizeMake(24, 24);
                    [_rightButton1 sizeToFit];
                    _rightButton1.centerY = self.backButton.centerY;
                    if(_rightButton2){
                        _rightButton1.left = _rightButton2.right + 28;
                    }
                    else{
                        _rightButton1.left =  16;
                    }
                }
            }
            if (_rightButton1) {
                if (self.backButton.left < _rightButton1.right+16) {
                    self.backButton.width = kScreenWidth - _rightButton1.right-16-16;
                    self.backButton.right = self.width - 16;
                }
            }
        }
        else{
            if (_rightButton1) {
                if (self.backButton.right > _rightButton1.left-16) {
                    self.backButton.width = kScreenWidth - 16 - (kScreenWidth - _rightButton1.left)-16;
                }
            }
            
            if (!_enableCustomFrameRightButtons) {
                if (_rightButton2) {
                    //            _rightButton2.size = CGSizeMake(24, 24);
                    [_rightButton2 sizeToFit];
                    _rightButton2.right = self.width - 16;
                    _rightButton2.centerY = self.backButton.centerY;
                }
                if (_rightButton1) {
                    //            _rightButton1.size = CGSizeMake(24, 24);
                    [_rightButton1 sizeToFit];
                    _rightButton1.centerY = self.backButton.centerY;
                    if(_rightButton2){
                        _rightButton1.right = _rightButton2.left - 28;
                    }
                    else{
                        _rightButton1.right = self.width - 16;
                    }
                }
                if (!_rightButton1 && !_rightButton2) {
                    if (self.backButton.right>self.width-16) {
                        self.backButton.width -= self.backButton.right-self.width+16;
                    }
                }
            }
        }
    } else {
        // Fallback on earlier versions
    }
    
}

- (UIButton*)backButton{
    if (!_backButton) {
        _backButton = [BackButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"back_ic"] forState:UIControlStateNormal];
        if (@available(iOS 9.0, *)) {
            if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
                [_backButton setImage:[UIImage imageNamed:@"back_ic"].imageByFlipHorizontal forState:UIControlStateNormal];
            }
        } else {
            // Fallback on earlier versions
        }
        _backButton.titleLabel.font = [UIFont systemFontOfSize:18];
        _backButton.left = 16;
        _backButton.bottom = self.height-8;
        [_backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:_backButton];
    }
    return _backButton;
}

- (UIButton*)rightButton1{
    if (!_rightButton1) {
        _rightButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton1.titleLabel.font = [UIFont systemFontOfSize:17];
        [self addSubview:_rightButton1];
    }
    return _rightButton1;
}

- (UIButton*)rightButton2{
    if (!_rightButton2) {
        _rightButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
       
        [self addSubview:_rightButton2];
    }
    return _rightButton2;
}

@end
